require 'rubygems'
require 'xml/libxml'
require 'family_tree_api/person/base'

module FamilyTreeApi
  module ValuesPerson
    NS = ['fs:http://api.familysearch.org/familytree/v1','fsapi:http://api.familysearch.org/v1']
    class Base < FamilyTreeApi::Person::Base
      
      def parse(p_element)
        p_element.children.each do |e|
          if e.name == 'values'
            e.children.each do |a|
              case a.name
              when 'name' then
                @names << FamilyTreeApi::Person::Assertion::Name.parse(a)
              when 'gender' then
                @genders << FamilyTreeApi::Person::Assertion::Gender.parse(a)
              when 'event' then
                @events << FamilyTreeApi::Person::Assertion::Event.parse(a)
              when 'fact' then
                @facts << FamilyTreeApi::Person::Assertion::Fact.parse(a)
              when 'relationship' then
                @relationships << FamilyTreeApi::Person::Assertion::Relationship.parse(a)
              end
            end
          end
        end
      end
      
      def self.parse(xml_str, ancestors = 0, descendants = 0)
        xp = XML::Parser.new
        xp.string = xml_str
        doc = xp.parse.root
        people = doc.find("/fs:familytree/fs:persons/fs:person",NS).collect do |p_element|
          person = self.new
          person.parse(p_element)
          person
        end
        people.first
      end
      
    end
  end
end